<?php
namespace app\admin\controller;
use app\admin\model\AuthRule as AuthRuleModel;
use app\admin\controller\Base;
use think\Db;
use app\admin\controller\Common;

class AuthRule extends Base
{
    public function lst(){
        $authRule=new AuthRuleModel();
        $authRuleRes=$authRule->authRuleTree();
        $this->assign('authRuleRes',$authRuleRes);
        return view();
    }

    public function add(){
        if($this->request()->isPost())
        {
            $data=input('post.');
            $add=db('auth_group')->insert($data);
            if($add){
                $this->success('添加用户组成功',url('list'));
            }
            else{
                $this->success('添加用户组失败',url('list'));
            }
            return;
        }
        return view();
    }

    public function ajaxadd()
    {
        $data=input('post.');
        $pid=input('pid');
        $rule=db('auth_rule')->where('id','=',$pid)->field('level')->find();

        if($pid==0){
            $data['level']=0;
        }
        else{
            $data['level']=$rule['level']+1;
        }
        $save=db('auth_rule')->insert($data);
        if($save){
            return ['ECODE'=>200,'MSG'=>'添加成功'];
        }
    }

    public function del()
    {
        $id=input('id');

            if (db('auth_rule')->where('id','=',$id)->delete()){
                $this->success('权限删除成功','lst');
            }else {
                $this->error('权限删除失败','lst');
            }


    }

    public function ajaxEdit(){
        $input = input();
        $res = db('auth_rule')->find($input['eid']);
        $prule = db('auth_rule')->find($input['pid']);
        if(!$prule){
            $prule['id']=0;
            $prule['title']=$res['title'];

        }


        $res['prule']=$prule;
        return $this->returnjson(200,$res);
    }

    public function ajaxseave()
    {
        $data=input('post.');
        $pid=input('pid');
        $rule=db('auth_rule')->where('id','=',$pid)->field('level')->find();

        if($pid==0){
            $data['level']=0;
        }
        else{
            $data['level']=$rule['level']+1;
        }
        $save=db('auth_rule')->update($data);
        if($save){
            return ['ECODE'=>200,'MSG'=>'修改成功'];
        }
    }



}
